package com.li.shop.controller;

import com.li.shop.domain.Message;
import com.li.shop.domain.Product;
import com.li.shop.domain.User;
import com.li.shop.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageController {
        @Autowired
        private MessageService messageService;

    @RequestMapping(value="/usermeslist",method= RequestMethod.GET)//查询一个用户的评论
    public String listUserMessage(Model model ,HttpSession session){
              User user=(User)session.getAttribute("session_user");
              String id=user.getuId();
              List<Message> mesList = messageService.findUserAllMessage(id);
              model.addAttribute("mesList",mesList);
              model.addAttribute(id);
//              System.out.println(mesList.get(0).getMessage());
//        System.out.println(mesList.get(1).getProduct().getProductPic());
//        System.out.println(mesList.get(2).getProduct().getProductPic());
//        System.out.println(mesList.get(0).getProduct().getProductPic());
        return "before/person/comment";
    }

        @RequestMapping(value="/messagelist",method= RequestMethod.GET)
        public String listMessage(Model model){
//             List<Message> list = messageService.findAll(id);
//            System.out.println(list.get(0).getLiuYans().get(0).getLiucontent());
//            System.out.println(list.get(0).getDate());
//            model.addAttribute("list", list);
//            System.out.println(list.size());
//            System.out.println(list);

            return "before/introduction";
        }
    @RequestMapping(value = "/addmessage", method = RequestMethod.GET)
    public String addMesasagePage(Model model, @RequestParam("id") String id, HttpSession session) {
        //model.addAttribute("message", new Message());
       // Message message=new Message();
        System.out.println("拿到的id为"+id);
        model.addAttribute("id",id);
        //session.setAttribute("msid",message.getMsId());
        User user=(User) session.getAttribute("session_user");
        model.addAttribute("uId",user.getuId());
        return "/back/messageadd";
    }



//    @RequestMapping(value = "/getupic", method = RequestMethod.GET)
//    public String getupic(Model model,@RequestParam("id") String id,HttpSession session) {
//        //model.addAttribute("message", new Message());
//        System.out.println("拿到的id为"+id);
//        model.addAttribute("id",id);
//        return "/back/messageadd";
//    }

    @RequestMapping(value = "/addmessage", method = RequestMethod.POST)
    public String saveMessage(@RequestParam("message") String message, @RequestParam("date") Date date, @RequestParam("pId") String pId, @RequestParam("uId") String uId, HttpSession session , Model model) {
            Message message1=new Message();
            message1.setMessage(message);
            message1.setpId(pId);
            message1.setuId(uId);
            message1.setDate(date);



        int updated = messageService.saveMessage(message1);
        System.out.println(message);

        Product product=(Product) session.getAttribute("product");
        //String pid=(String)session.getAttribute("pid");
        // String ppid=product.getpId();
        //System.out.println(ppid);
        System.out.println(product.getpId());
        //model.addAttribute("id",id);
        return "redirect:/product/detail?id="+product.getpId()+"";
        //return "redirect:/user/login";
        // return "redirect:/message/messagelist";
    }


    @RequestMapping (value = "/deleteusermes",method=RequestMethod.GET)
    public String deleteMerchants(@RequestParam("id") String id) {
        int deleted = messageService.deleteUserMesByPrimaryKey(id);
        return "redirect:/message/usermeslist";
    }
}
